import { request } from '@umijs/max';

// 房屋详情数据类型定义
export interface HouseDetailData {
  id: string;
  basicInfo: {
    cityName: string;
    communityName: string;
    address: string;
    houseInfo: string;
    area: number;
    buildingType: string;
    floor: number;
    totalFloors: number;
    orientation: string;
    decoration: string;
    buildYear: number;
    propertyType: string;
    propertyRight: string;
    createTime: string;
    updateTime: string;
  };
  rentalInfo: {
    rentalType: 'whole' | 'shared';
    status: string;
    wholeRental: {
      monthlyRent: number;
      paymentCycle: string;
      rentalStatus: string;
      viewingPeriod: string;
      moveInDate: string;
      tenantInfo: {
        name: string;
        phone: string;
        idCard: string;
        contractStartDate: string;
        contractEndDate: string;
      };
    };
    sharedRooms: Array<{
      id: string;
      name: string;
      roomType: string;
      orientation: string;
      area: number;
      monthlyRent: number;
      status: string;
      tenantInfo: {
        name: string;
        phone: string;
        contractStartDate: string;
        contractEndDate: string;
      } | null;
    }>;
  };
  businessInfo: {
    propertyInfo: {
      hasPropertyInfo: boolean;
      propertyOwner: string;
      propertyCertificate: string;
      propertyArea: number;
      propertyType: string;
      propertyRight: string;
      purchaseDate: string;
      purchasePrice: number;
    };
    authInfo: {
      hasAuthInfo: boolean;
      authStatus: string;
      authDate: string;
      authPerson: string;
      authCertificate: string;
      authPhotos: string[];
    };
    contractInfo: {
      contractAmount: number;
      contractStartDate: string;
      contractEndDate: string;
      serviceFee: number;
      paymentCycle: string;
    };
    contracts: Array<{
      id: string;
      type: string;
      tenantName: string;
      startDate: string;
      endDate: string;
      amount: number;
      status: string;
      signDate: string;
    }>;
    bills: Array<{
      id: string;
      type: string;
      amount: number;
      dueDate: string;
      status: string;
      payDate: string | null;
      description: string;
    }>;
    repairs: Array<{
      id: string;
      type: string;
      description: string;
      reportDate: string;
      status: string;
      completeDate: string;
      cost: number;
      repairman: string;
    }>;
    transfers: Array<{
      id: string;
      type: string;
      tenantName: string;
      applyDate: string;
      status: string;
      reason: string;
      expectedDate: string;
    }>;
    logs: Array<{
      id: string;
      action: string;
      operator: string;
      time: string;
      description: string;
    }>;
  };
}

// API响应类型
export interface ApiResponse<T> {
  code: number;
  message: string;
  data: T;
}

/**
 * 获取房屋详情
 * @param id 房屋ID
 */
export async function getHouseDetail(id: string): Promise<HouseDetailData> {
  const response = await request<ApiResponse<HouseDetailData>>(
    `/api/house/detail/${id}`,
    {
      method: 'GET',
    },
  );

  if (response.code === 200) {
    return response.data;
  }

  throw new Error(response.message || '获取房屋详情失败');
}

/**
 * 更新房屋基础信息
 * @param id 房屋ID
 * @param data 基础信息数据
 */
export async function updateHouseBasicInfo(
  id: string,
  data: any,
): Promise<void> {
  const response = await request<ApiResponse<{ id: string }>>(
    `/api/house/detail/${id}/basic`,
    {
      method: 'PUT',
      data,
    },
  );

  if (response.code !== 200) {
    throw new Error(response.message || '更新房屋基础信息失败');
  }
}

/**
 * 更新出租信息
 * @param id 房屋ID
 * @param data 出租信息数据
 */
export async function updateHouseRentalInfo(
  id: string,
  data: any,
): Promise<void> {
  const response = await request<ApiResponse<{ id: string }>>(
    `/api/house/detail/${id}/rental`,
    {
      method: 'PUT',
      data,
    },
  );

  if (response.code !== 200) {
    throw new Error(response.message || '更新出租信息失败');
  }
}
